package com.code.common.utils;


import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.util.Random;

/**
 * MD5 加密工具类
 * */
public class MD5Util {

    /**
     * md5密码加密工具类
     *
     * @param credentials 密码
     * @param saltSource 密码盐
     * @return
     */
    public static String md5(String credentials, String saltSource) {
        ByteSource salt = new Md5Hash(saltSource);
        return new SimpleHash("MD5", credentials, salt, 1024).toString();
    }

    /**
     * 获取随机盐值
     * @param length
     * @return
     */
    public static String getRandomSalt(int length) {
        return getRandomString(length);
    }

    /**
     * 获取随机位数的字符串
     */
    public static String getRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }



    public static void main(String[] args) {
        String in_pwd = md5("zhangy@1235","74a38210d4e3450748bb1d017457ff01");
        String out_pwd =  "03a74659f619caa5e188e6826b462bec";

        if(in_pwd.equals(out_pwd)) {
            System.out.println("eq");
        } else {
            System.out.println(" not eq");
        }

        //System.out.println("-------------------------------");
        // String salt = getRandomSalt(6);
        //  System.out.println("--salt--"+salt);
        // String getpwd = md5("123456",salt);
        // System.out.println("--getpwd--"+getpwd);

    }








}
